import BaseConfig from "../../Script/Base/BaseConfig";
import BaseSprite from "../../Script/Base/BaseSprite";
import { EBundleType, EViewType } from "../../Script/Common/Enum";
import { IViewData } from "../../Script/Interface/IMessage";
import { ELayerType, SceneGame } from "../../Script/SceneGame";
import AppService from "../../Script/Service/AppService";
import AudioManager from "../../Script/Util/AudioManager";
import StorageUtil from "../../Script/Util/StorageUtil";
import BaseLayer from "../Base/BaseLayer";
import EventID from "../Common/EventID";
import Message from "../Common/Message";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'LayerLoad',
    type: EViewType.Layer,
    bundle: EBundleType.Load,
    zIndex: 100,
};

@ccclass
export class LayerLoad extends BaseLayer {

    @property({ type: cc.ProgressBar, tooltip: "进度条bar" })
    private progressBar: cc.ProgressBar = null;

    @property({ type: cc.Node, tooltip: "进度条特效" })
    private barEffectNode: cc.Node = null;

    @property({ type: cc.Label, tooltip: "进度条进度文本" })
    private labProgress: cc.Label = null;

    private currentValue: number = 0;
    private maxProgressValue: number = 1;

    private index: number = 0;
    private updateWidth: number = 1;
    private isfirst: boolean = true;

    protected onLoad() {
        super.onLoad();        
        StorageUtil.instance.get();
        BaseConfig.instance.initJsonInfos();
        AudioManager.instance.initAudioInfos();
    }

    protected start() {
        // // 游戏启动黑屏处理
        // HttpManager.callAndroidMethod(EAndroidType.GameBlackSolve);
        // console.log("LayerLoad.start()===>游戏启动黑屏处理");

        // const gameData = StorageUtil.instance.getGameData();
        // this.talkData.id = gameData?.talkData?.id || -1;
    }

    private progress(updateWidth: number, stopPro: number, isComplete: boolean) {
        updateWidth = isComplete ? updateWidth * 0.3 : updateWidth;
        if (isComplete) {
            this.currentValue += updateWidth;
            if (this.currentValue + updateWidth >= stopPro / 100 * this.maxProgressValue) {
                this.currentValue = stopPro / 100 * this.maxProgressValue;
                this.index++;
            }
        } else {
            this.currentValue += updateWidth;
            if (this.currentValue + updateWidth >= stopPro / 100 * this.maxProgressValue) {
                this.currentValue = stopPro / 100 * this.maxProgressValue;
            }
        }
    }

    protected update(dt) {
        const updateWidth = this.updateWidth * dt;
        switch (this.index) {
            case 0: // 加载配置表数据
                {
                    const isComplete = BaseConfig.instance.isLoadComplete;
                    this.progress(updateWidth, 33, isComplete);
                }
                break;
            case 1: // 加载音乐音效
                {
                    const isComplete = AudioManager.instance.isLoadComplete;
                    this.progress(updateWidth, 88, isComplete);
                }
                break;
            case 2: // 正常加载
                {
                    this.progress(updateWidth, 100, true);
                }
                break;
            case 3: // 进入游戏
                {
                    if (this.isfirst) {
                        this.isfirst = false;
                        const gameData = StorageUtil.instance.getGameData();
                        const talkData = StorageUtil.instance.getTalkData();
                        const isPlay = Boolean(gameData.isPlayMusic);
                        AudioManager.instance.setEnabled(isPlay);
                        if (talkData.id <= 0) {
                            SceneGame.instance.showLayer(ELayerType.LayerStory1);
                        } else {
                            SceneGame.instance.showLayer(ELayerType.LayerGame);
                        }
                        // SceneGame.instance.showLayer(ELayerType.LayerGame);
                    }
                }
                break;
        }

        this.labProgress.string = `加载${Math.floor(this.currentValue * 100)}%`;
        this.progressBar.progress = this.currentValue;
        // this.barEffectNode.x = -250 + this.currentValue * 500 - 5;
        // if (this.barEffectNode.x >= 220) {
        //     this.barEffectNode.x = 220;
        // }
    }

    private onCleanData() {
        StorageUtil.instance.clear();
        SceneGame.instance.showLayer(ELayerType.LayerLoad);
        Message.instance.send(EventID.REFRESH_TOP_BAR);
    }

    public getViewData() {
        return VIEW_DATA;
    }
}
AppService.instance.registViewData(VIEW_DATA);